<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--商品分类-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入学习分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="categoryOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <!-- <el-form-item label="商品ID" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入商品ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品分类表主键" prop="categoryId">
        <el-input
          v-model="queryParams.categoryId"
          placeholder="请输入商品分类表主键"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <el-form-item label="商品名称" prop="goodsName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入商品名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="图片地址" prop="goodsImg">
        <el-input
          v-model="queryParams.goodsImg"
          placeholder="请输入图片地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <!-- <el-form-item label="支付规则 (0：积分 1：现金 2：积分+现金）" prop="payRule">
        <el-input
          v-model="queryParams.payRule"
          placeholder="请输入支付规则 (0：积分 1：现金 2：积分+现金）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <el-form-item label="商品积分数" prop="goodsScore">
            <el-input
              v-model="queryParams.goodsScore"
              placeholder="请输入商品积分数"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="商品现金" prop="goodsMoney">
            <el-input
              v-model="queryParams.goodsMoney"
              placeholder="请输入商品现金"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="删除标志(0：未删除 1：已删除)" prop="yn">
        <el-input
          v-model="queryParams.yn"
          placeholder="请输入删除标志(0：未删除 1：已删除)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['customer:mallGoods:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['customer:mallGoods:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['customer:mallGoods:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['customer:mallGoods:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="mallGoodsList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="${comment}" align="center" prop="id" />
      <el-table-column label="商品ID" align="center" prop="goodsId" />
      <el-table-column label="商品分类表主键" align="center" prop="categoryId" /> -->
          <el-table-column label="商品名称" align="center" prop="goodsName" />
          <el-table-column label="商品简称" align="center" prop="goodsShortName" />
          <el-table-column label="图片地址" align="center" prop="goodsImg">
            <template slot-scope="scope">
              <div class="img-preview">
                <img class="imgInfo" :src="`${baseUrl}${scope.row.goodsImg}`" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="商品描述" align="center" prop="goodsDesc" />
          <el-table-column label="商品使用说明" align="center" prop="goodsInstructions" />
          <el-table-column
            label="支付规则 (0：积分 1：现金 2：积分+现金）"
            align="center"
            prop="payRule"
          />
          <el-table-column
            label="商品类型 (0：家具 1：商品 2：智能对话包）"
            align="center"
            prop="goodsType"
          />
          <el-table-column label="商品积分" align="center" prop="goodsScore" />
          <el-table-column label="商品现金" align="center" prop="goodsMoney" />
          <el-table-column label="商品状态" align="center" prop="goodsStatus" />
          <el-table-column
            label="删除标志(0：未删除 1：已删除)"
            align="center"
            prop="yn"
          />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['customer:mallGoods:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['customer:mallGoods:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改商城商品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="商品ID" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="商品分类表主键" prop="categoryId">
          <el-input v-model="form.categoryId" placeholder="请输入商品分类表主键" />
        </el-form-item> -->
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品简称" prop="goodsShortName">
          <el-input v-model="form.goodsShortName" placeholder="请输入商品简称" />
        </el-form-item>
        <el-form-item label="图片地址" prop="goodsImg">
          <ImageUpload @success="uploadImg" :limit="1" :value="form.goodsImg" />
        </el-form-item>
        <el-form-item label="小图片地址" prop="goodsSmallImg">
          <ImageUpload @success="uploadSmallImg" :limit="1" :value="form.goodsSmallImg" />
        </el-form-item>
        <el-form-item label="大图片地址" prop="goodsBigImg">
          <ImageUpload @success="uploadBigImg" :limit="1" :value="form.goodsBigImg" />
        </el-form-item>
        <el-form-item label="商品描述" prop="goodsDesc">
          <el-input v-model="form.goodsDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品使用说明" prop="goodsInstructions">
          <el-input
            v-model="form.goodsInstructions"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="支付规则" prop="payRule">
          (0：积分 1：现金 2：积分+现金）
          <el-select
            v-model="form.payRule"
            placeholder="请选择支付类型"
            @change="changePayRule"
          >
            <el-option
              v-for="dict in dict.type.goods_pay_rule"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品类型" prop="goodsType">
          (0, "家具"), (1, "商品"), (2, "智能对话包")
          <el-select
            v-model="form.goodsType"
            placeholder="请选择商品类型"
          >
            <el-option
              v-for="dict in dict.type.goods_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上下架" prop="goodsStatus">
          <el-radio-group v-model="form.goodsStatus" size="medium">
            <el-radio
              v-for="(item, index) in statusOption"
              :key="index"
              :label="item.value"
              :disabled="item.disabled"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item v-show="showScore" label="商品积分数" prop="goodsScore">
          <el-input-number
            v-model="form.goodsScore"
            :min="1"
            :max="1000"
            placeholder="请输入商品积分数"
          />
          <!-- <el-input v-model="form.goodsScore" placeholder="请输入商品积分数" /> -->
        </el-form-item>
        <el-form-item v-show="showMoney" label="商品现金" prop="goodsMoney">
          <el-input-number
            v-model="form.goodsMoney"
            :min="1"
            :max="1000"
            placeholder="请输入商品现金"
          />
          <!-- <el-input v-model="form.goodsMoney" placeholder="请输入商品现金" /> -->
        </el-form-item>
        <!-- <el-form-item label="删除标志(0：未删除 1：已删除)" prop="yn">
          <el-input v-model="form.yn" placeholder="请输入删除标志(0：未删除 1：已删除)" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMallGoods,
  getMallGoods,
  delMallGoods,
  addMallGoods,
  updateMallGoods,
} from "@/api/customer/mallGoods";
import { mallGoodsCategoryTree } from "@/api/customer/mallGoodsCategory";

export default {
  name: "MallGoods",
  dicts: ["goods_pay_rule", "goods_type"],
  data() {
    return {
      //显示积分输入框
      showScore: false,
      //显示现金输入框
      showMoney: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城商品信息表格数据
      mallGoodsList: [],
      categoryOptions: [],
      //商品上架下架
      statusOption: [
        {
          label: "下架",
          value: "DownShelves",
        },
        {
          label: "上架",
          value: "UpShelves",
        },
      ],
      //学习库分类名称
      categoryName: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      selectCategoryId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        categoryId: null,
        goodsName: null,
        goodsShortName: null,
        goodsImg: null,
        goodsSmallImg: null,
        goodsBigImg: null,
        goodsDesc: null,
        goodsInstructions: null,
        payRule: null,
        goodsScore: null,
        goodsMoney: null,
        goodsStatus: null,
        goodsType: null,
        yn: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsName: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
        goodsShortName: [
          { required: true, message: "商品简称不能为空", trigger: "blur" },
        ],
        goodsImg: [{ required: true, message: "图片地址不能为空", trigger: "blur" }],
        goodsDesc: [{ required: true, message: "商品描述不能为空", trigger: "blur" }],
        goodsInstructions: [
          { required: true, message: "商品使用说明不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    categoryName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getMallGoodsCategoryTreeList();
  },
  methods: {
    /** 查询商城商品信息列表 */
    getList() {
      this.loading = true;
      listMallGoods(this.queryParams).then((response) => {
        this.mallGoodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getMallGoodsCategoryTreeList() {
      mallGoodsCategoryTree().then((response) => {
        this.categoryOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        categoryId: null,
        goodsName: null,
        goodsShortName: null,
        goodsImg: null,
        goodsSmallImg: null,
        goodsBigImg: null,
        goodsDesc: null,
        goodsInstructions: null,
        payRule: null,
        goodsScore: null,
        goodsMoney: null,
        goodsStatus: null,
        goodsType: null,
        yn: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (this.selectCategoryId == null) {
        this.$modal.msgError("请选择商品分类");
        return;
      }
      this.reset();
      this.open = true;
      this.title = "添加商城商品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (this.selectCategoryId == null) {
        this.$modal.msgError("请选择商品分类");
        return;
      }
      this.reset();
      const id = row.id || this.ids;
      getMallGoods(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商城商品信息";
      });
    },
    handleNodeClick(data) {
      debugger;
      this.queryParams.categoryId = data.id;
      this.selectCategoryId = data.id;
      this.handleQuery();
    },
    changePayRule(row) {
      if (row == "Score") {
        this.showScore = true;
        this.showMoney = false;
      }
      if (row == "Money") {
        this.showScore = false;
        this.showMoney = true;
      }
      if (row == "ScoreAndMoney") {
        this.showScore = true;
        this.showMoney = true;
      }
    },
    uploadImg(file) {
      this.form.goodsImg = file;
    },
    uploadSmallImg(file) {
      this.form.goodsSmallImg = file;
    },
    uploadBigImg(file) {
      this.form.goodsBigImg = file;
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.goodsName.length > 10) {
        this.$modal.msgError("商品名称不能大于10个字");
        return;
      }
      if (this.form.goodsShortName > 3) {
        this.$modal.msgError("商品简称不能大于3个字");
        return;
      }
      if (this.selectCategoryId == null) {
        this.$modal.msgError("请选择商品分类");
        return;
      }
      if (this.form.goodsStatus == null) {
        this.$modal.msgError("请选择上架下架状态");
        return;
      }
      this.form.categoryId = this.selectCategoryId;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMallGoods(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallGoods(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商城商品信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMallGoods(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "customer/mallGoods/export",
        {
          ...this.queryParams,
        },
        `mallGoods_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.img-preview {
  width: 200px;
  height: 120px;
  // position: relative;
  // text-align: center;

  // .img {
  //   max-width: 100%;
  //   max-height: 100%;
  //   border-radius: 2px;
  // }
}
.imgInfo {
  width: auto;
  max-width: 100%;
  max-height: 100%;
}
</style>
